home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / prolog_2.zip / PUZZLES.ZIP / APPENDS.PRO < prev    next >
Text File  |  1986-07-20  |  3KB  |  52 lines

  1. /*
  2.     Note from Bob Morein: This file contributed by Steve Weaver  
  3. from Logicon, Inc., who created the art-deco comment style. The
  4. "applist" predicate is available in type FS and above. However, 
  5. you can work with open lists without it. Suggested reference:
  6. "Prolog for Programmers", by Kluzniak, Academic Press studies 
  7. in Data Processing #24, 1985, which is also the best logic 
  8. programming textbook I have seen, and much better than C & M.
  9.  
  10. The purpose of this file is to show how open lists can be appended
  11. faster and more effectively than closed, "proper" lists, which are
  12. the only types discussed in C & M. Examples of improper lists are
  13. [a,b,c|3], or [a,b,c|X]. A list which terminates in a variable can
  14. have another list appended to the tail without copying, which is
  15. an inefficiency of the "append" predicate described in C & M. 
  16. */
  17.  
  18.  /****************************************************************************/
  19.  
  20.  /***** Append two lists to create a third list                          *****/
  21.  /*****   Converts input lists to open lists, does fast append,          *****/
  22.  /*****      then closes resulting list.                                 *****/
  23.  /*****     1st arg is first list                                        *****/
  24.  /*****     2nd arg is second list                                       *****/
  25.  /*****     3rd arg is an openlist whose value is first appended to second ***/
  26.  /*****                                                                  *****/
  27.  /***** NOTE:  operator '--'MUST BE DECLARED:                            *****/
  28. ?-op(240,xfx,'--').                                  
  29.  /****************************************************************************/
  30. append(L1,L2,L3) 
  31.       :-    applist(L1,X,OL1),
  32.             applist(L2,Y,OL2),
  33.             d_conc(OL1--X,OL2--Y,L3--[]),
  34.             !.       
  35.     append(L1,L2,L3)
  36.       :-    apndslow(L1,L2,L3),
  37.             !.
  38.     apndslow([],L,L)
  39.       :-    !.
  40.     apndslow([F|R],L2,[F|New_List])
  41.       :-    apndslow(R,L2,New_List),
  42.            !.                         
  43.  /****************************************************************************/
  44.  /***** append two open lists to create a third open list                *****/
  45.  /*****     1st arg is first openlist                                    *****/
  46.  /*****     2nd arg is second openlist                                   *****/
  47.  /*****     3rd arg is an openlist whose value is first appended to second ***/
  48.  /****************************************************************************/
  49.  
  50. d_conc(List1 -- Tail1,Tail1 -- Tail2,List1 -- Tail2).  
  51.  
  52.